package com.hy.mydesktop.client.mvc.form;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.desktop.client.Desktop;
import com.extjs.gxt.desktop.client.Shortcut;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.hy.mydesktop.client.component.event.AppEvents;
import com.hy.mydesktop.client.component.factory.GxtComponentFactory;
import com.hy.mydesktop.client.component.meta.GxtComponentMetaModel;
import com.hy.mydesktop.client.component.meta.GxtComponentMetaNodeModel;
import com.hy.mydesktop.client.component.type.ComponentTypeEnum;
import com.hy.mydesktop.client.mvc.base.BaseController;
import com.hy.mydesktop.client.mvc.desktop.DesktopView;

public class FormSetModuleView extends View{

	private Desktop desktop = null;
	private FormPanel formPanel = null;
	
	public FormPanel getFormPanel() {
		return formPanel;
	}

	public void setFormPanel(FormPanel formPanel) {
		this.formPanel = formPanel;
	}

	public FormSetModuleView(Controller controller) {
		super(controller);
	}

	@Override
	protected void handleEvent(AppEvent event) {
		
		if (desktop==null) {
			//desktop = (Desktop)map.get("desktop");
			desktop = Registry.get("desktop");
		}		
		//读取事件中的所有数据信息
		 Map<String, Object> map = event.getData();
		
		//0007
		if (AppEvents.DESKTOP_WINDOWAPPLICATION_INIT==event.getType()) {
			
			String viewDataCode=((BaseController)this.getController()).getViewData();
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel = null;
			if (viewDataCode!=null) {
				/**
				 * 调用后台业务逻辑，获得viewDataCode，对应的元模型
				 */
				System.out.println("viewDataCode代码是："+viewDataCode+"执行getGxtComponentMetaNodeModel ()");
				if (viewDataCode.equals("menuBar_viewData_init_01")) {
				//	gxtComponentMetaNodeModel = this.getGxtComponentMetaNodeModel();
				}
				
				
				
			}
			//gxtComponentMetaNodeModel = this.getGxtComponentMetaNodeModel();
			gxtComponentMetaNodeModel = ((BaseController)(this.getController())).getGxtComponentMetaNodeModel();

			/**
			 * 三个参数
			 * 两个参数，一个返回值
			 */
			GxtComponentMetaModel gxtComponentMetaModel = new GxtComponentMetaModel();
			List<GxtComponentMetaNodeModel> list=new LinkedList<GxtComponentMetaNodeModel>();
			list.add(gxtComponentMetaNodeModel);
			gxtComponentMetaModel.setList(list);
			
			formPanel = GxtComponentFactory.createGxtComponent(gxtComponentMetaModel);
		   
			BaseController parentController =(BaseController)(((BaseController)this.getController()).getParent());
			Map<BaseController, Component> controllerandcomponent = (Map<BaseController, Component>)map.get("controllerandcomponent");
			LayoutContainer layoutContainer = (LayoutContainer) controllerandcomponent.get(parentController);
			
		    if (layoutContainer!=null) {
		    	//layoutContainer.add(new Button("xxxxxxxxx"));
		    	layoutContainer.add(formPanel);
			}
		    System.err.println("formPanel 的parent"+layoutContainer);
		    
		};
		
		
		//根据事件类型和事件中包含的数据，响应事件
		if(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT==event.getType()){//根据事件类型，处理事件
			//根据事件中包含的数据，响应事件，即创建并初始化窗口
			
		//	this.createDynamicDesktop2(event);//最终创建出了桌面快捷方式（View视图）
			/**
			 * 何时添加控件的事件
			 * 1：创建控件的过程中，添加
			 * 识别GxtComponentMetaNodeModel中，Map中某个指，来设置它是否有AppEvent事件，
			 * 如何有AppEvent事件，需要被监听。
			 * 需要在Shortcut工厂中，通过ShortcutModelEnum.AppEven来判断，是否有事件需要监听
			 * 如果判断有，
			 * 根据Shortcut特定的方式，添加事件监听。如 Shortcut.addListener()
			 * 
			 * 2：控件创建完成后，添加
			 * 控件创建完成后，返回的是一个Desktop。
			 * 如何知道，改为哪个控件添加事件
			 */
			/**
			 * 为每个快捷方式，注册单击事件（包括业务事件）
			 */
			//List<Shortcut> list = desktop.getShortcuts();
			//list.get(0).
		}
	}
	
	private GxtComponentMetaModel getDesktopMetaModel() {
		Map<String, Object> map1=new HashMap<String, Object>();
		map1.put("component_model_type", ComponentTypeEnum.DESKTOP_MODEL);
		GxtComponentMetaNodeModel node1=new GxtComponentMetaNodeModel(map1, 1, 6);
		
		Map<String, Object> map2=new HashMap<String, Object>();
		map2.put("component_model_type", ComponentTypeEnum.SHORTCUT_MODEL);
		//TODO map2.put("appEventType", AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT);
		map2.put("appEventType", AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT);
		GxtComponentMetaNodeModel node2=new GxtComponentMetaNodeModel(map2, 2, 3);
		
		Map<String, Object> map3=new HashMap<String, Object>();
		map3.put("component_model_type", ComponentTypeEnum.SHORTCUT_MODEL);
		GxtComponentMetaNodeModel node3=new GxtComponentMetaNodeModel(map3, 4, 5);
		
		List<GxtComponentMetaNodeModel> list = new ArrayList<GxtComponentMetaNodeModel>();
		list.add(node1);
		list.add(node2);
		list.add(node3);
		GxtComponentMetaModel gxtComponentMetaModel = new GxtComponentMetaModel();
		gxtComponentMetaModel.setList(list);
		
		return gxtComponentMetaModel;
		
	}
	
	
	 /**
	 * 模拟从后台传过来的关于Window控件的元模型信息
	 */
	private GxtComponentMetaNodeModel getGxtComponentMetaNodeModel2() {
		
		
		
		
		
		Map<String, Object> MaxPanel=new HashMap<String, Object>();
		MaxPanel.put("component_model_type", ComponentTypeEnum.FORMPANEL_MODEL);
		MaxPanel.put("autowidth", true);
		MaxPanel.put("autoheight", true);
		MaxPanel.put("frame", false);
		MaxPanel.put("headervisible", false);
		MaxPanel.put("width", 1000);
			/*//编号
			Map<String, Object> id = new HashMap<String, Object>();
			id.put("component_model_type", ComponentTypeEnum.FORM_MODULE_MODEL);
			id.put("autowidth", true);
			id.put("autoheight", true);
			id.put("frame", true);
			id.put("headervisible", false);
			GxtComponentMetaNodeModel node10  = new GxtComponentMetaNodeModel(id,8,9);
			
			
			
			List<GxtComponentMetaNodeModel> subList = new LinkedList<GxtComponentMetaNodeModel>();
			subList.add(node10);
		MaxPanel.put("formitemlist", subList);*/
		MaxPanel.put("layout", ComponentTypeEnum.COLUMNLAYOUT_MODEL);
			//创建提交和取消按钮
//			Map<String, Object> btnSubmit=new HashMap<String, Object>();
//			btnSubmit.put("component_model_type", ComponentTypeEnum.BUTTON_MODEL);
//			btnSubmit.put("text", "确定");
//			GxtComponentMetaNodeModel btnSubmit1=new GxtComponentMetaNodeModel(btnSubmit);
//			
//			Map<String, Object> btnquxiao=new HashMap<String, Object>();
//			btnquxiao.put("component_model_type", ComponentTypeEnum.BUTTON_MODEL);
//			btnquxiao.put("text", "取消");
//			GxtComponentMetaNodeModel btn1=new GxtComponentMetaNodeModel(btnquxiao);
//			
//			List<GxtComponentMetaNodeModel> list1=new ArrayList<GxtComponentMetaNodeModel>();
//			list1.add(btnSubmit1);
//			list1.add(btn1);
//		MaxPanel.put("addbutton", list1);
		MaxPanel.put("buttonalign", "CENTER");
		GxtComponentMetaNodeModel node5=new GxtComponentMetaNodeModel(MaxPanel);
		
		return node5;
	}
	
	 
	
}
